/*
 * All rights Reserved, Designed By www.goldbeacon.cn
 * @Title:  demo
 * @Package cn.goldbeacon.demo.web
 * @author: zoujie
 * @date:   2021/7/219:13
 * @version V1.0
 * Copyright © 2021 www.goldbeacon.cn. All Rights Reserved.
 * 注意：本内容仅限于湖南金烽信息科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.chenzhengrong.demospringboot.controlloer;

/*import cn.goldbeacon.demo.domain.dto.RegionDTO;
import cn.goldbeacon.demo.domain.vo.RegionVO;
import cn.goldbeacon.demo.manager.RegionManager;
import cn.goldbeacon.demo.service.RegionService;*/
import com.alibaba.fastjson.JSON;
import com.chenzhengrong.demospringboot.annotation.Log;
import com.chenzhengrong.demospringboot.domain.R;
import com.chenzhengrong.demospringboot.domain.dto.RegionDTO;
import com.chenzhengrong.demospringboot.domain.pojo.Region;
import com.chenzhengrong.demospringboot.domain.vo.RegionVO;
import com.chenzhengrong.demospringboot.manager.RegionManager;
import com.chenzhengrong.demospringboot.service.RegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author zoujie
 * @title: PCASCodeController
 * @description: 省市区数据访问控制层
 * @date 2021/7/219:13
 */
@Log4j2
@RestController
@Api(tags = "省市区数据管理")
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private RegionManager regionManager;

    @Autowired
    private RegionService regionService;


    /**
     * @description: 初始化数据
     * @return java.lang.String
     * @author zoujie
     * @date 2021/7/21 13:08
     */
    @ApiOperation(value = "初始化省市区数据")
    @RequestMapping(value="/init", method= RequestMethod.GET)
    public String init() {
        try {
            URL resource = RegionController.class.getResource("/pcas-code.json");
            assert resource != null;
            String decode = URLDecoder.decode(resource.getFile(), "UTF-8");
            String json = FileUtils.readFileToString(new File(decode), Charset.defaultCharset());
            List<RegionVO> regionVOS = JSON.parseArray(json, RegionVO.class);

            List<RegionDTO> regionDTOS = regionManager.readTree(regionVOS, "0");
            regionManager.batchInsert(regionDTOS);
            return "success";
        } catch (Exception e) {
            log.error("初始化数据出错，原因：{}", e.getMessage(), e);
            return "failed";
        }
    }

    /*
    * @description: 根据地区名称查询地区数据
    * @param name 地区名称
    * @return cn.goldbeacon.demo.domain.vo.RegionVO
    * @author zoujie
    * @date 2021/7/21 14:54
     */
    @ApiOperation(value = "根据地区名查询地区数据")
    @GetMapping("/query/{name}")
    public R<RegionVO> query(@PathVariable("name") String name) throws Exception {
        /*try {
            RegionVO region = regionManager.query(name);
            log.info("region：{}", region);
            return region;
        } catch (Exception e) {
            log.error("查询数据失败，原因：{}", e.getMessage(), e);
            return null;
        }*/
        RegionVO query = regionManager.query(name);
        return R.ok(query);
    }

    /*
    * @description: 根据省份名称查询省份下所有城市的数据
    * @param provinceName 省份名称
    * @return java.util.List<cn.goldbeacon.demo.domain.vo.RegionVO>
    * @author zoujie
    * @date 2021/7/22 14:35
    */
    @ApiOperation(value = "根据省份名称查询省份下所有城市的数据")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public List<RegionVO> queryCityByProvince(String provinceName) {
        return regionManager.queryCityByProvince(provinceName);
    }

    /*
    * @description: 添加城市数据
    * @param regionDTO 地区数据
    * @author zoujie
    * @date 2021/7/22 14:36
    */
    @ApiOperation("添加城市数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String create(RegionDTO regionDTO) {
        int count = regionService.insert(regionDTO);
        return count == 1 ? "success" : "failed";
    }

    /*
    * @description: 更新数据
    * @param regionDTO 地区数据
    * @return void
    * @author zoujie
    * @date 2021/7/22 14:38
    */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public String edit(RegionDTO regionDTO) {
        int count = regionService.updateRegion(regionDTO);
        return count == 1 ? "success" : "failed";
    }

    /*
    * @description: 根据Id删除数据
    * @param id
    * @return void
    * @author zoujie
    * @date 2021/7/23 10:35
    */
    /*@RequestMapping(value = "/delete{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") Long id) {
        int count = regionService.delete(id);
        return count == 1 ? "success" : "failed";
    }*/

    /*
     * @description: TODO
     * @param name
     * @return com.chenzhengrong.demospringboot.domain.pojo.Region
     * @throws
     * @author chenzhengrong
     * @date 2022/8/17 13:33
     */
    /*@GetMapping("/query/{name}")
    public Region findRegionByName(@PathVariable("name") String name) {
        Region region = regionService.findByName(name);
        log.info("region: {}", region);
        return region;
    }*/

}
